package com.nxmu.hire.controller;

import com.github.pagehelper.PageInfo;
import com.nxmu.hire.common.constant.SystemConstant;
import com.nxmu.hire.common.constant.UserConstant;
import com.nxmu.hire.common.util.Page;
import com.nxmu.hire.common.util.ResponseResult;
import com.nxmu.hire.model.Publicity;
import com.nxmu.hire.model.User;
import com.nxmu.hire.service.PublicityService;
import com.nxmu.hire.vo.AddPublicityReqVo;
import com.nxmu.hire.vo.FindPublishByPageReqVo;
import com.nxmu.hire.vo.FindPublishByPageResVo;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.io.File;
import java.util.List;
import java.util.UUID;

@CrossOrigin(origins = "*",allowCredentials = "true")
@RestController
@RequestMapping("/publicity")
public class PublicityController {

    private final static Logger LOGGER = LoggerFactory.getLogger(PublicityController.class);

    @Autowired
    private PublicityService publicityService;

    @ApiOperation("添加发布信息")
    @PostMapping(value = "/addPublicity")
    public ResponseResult addPublicity(@Valid @RequestBody AddPublicityReqVo addPublicity, HttpServletRequest request){
        Publicity publicity = new Publicity();
        BeanUtils.copyProperties(addPublicity,publicity);
        MultipartFile file = addPublicity.getFile();

        publicity.setPublisher(((User)request.getSession().getAttribute(UserConstant.SESSION_USER)).getUserName());
        publicity.setAdditionPath(file.getName());
        publicity.setAdditionName(file.getOriginalFilename());

        // 目标文件路径及文件名
        String targetFilePath = request.getServletContext().getRealPath(SystemConstant.PUBLISH_PATH);
        String targetFileName = UUID.randomUUID().toString() + System.currentTimeMillis() + StringUtils.getFilenameExtension(file.getName());
        File targetFile = new File(targetFilePath + targetFileName);

        try {
            publicityService.save(publicity,file.getInputStream(),targetFile);
        } catch (Exception e) {
            LOGGER.error(e.getMessage());
            return ResponseResult.fail(e.getMessage());
        }
        return ResponseResult.success();
    }

    @ApiOperation(value = "发布信息查询")
    @PostMapping(value = "/findPublishByPage")
    public ResponseResult findPublishByPage(@Valid @RequestBody FindPublishByPageReqVo findPublishByPageReqVo){

        PageInfo<Publicity> pageInfo = publicityService.findPublishByPageAndCondition(
                findPublishByPageReqVo.getHeadLine(),
                findPublishByPageReqVo.getPageNum(),
                findPublishByPageReqVo.getPageSize());

        List<FindPublishByPageResVo> resultList = publicityService.converterPublicityListToFindPublishByPageResVoList(pageInfo.getList());

        Page page = new Page();
        BeanUtils.copyProperties(pageInfo,page);
        return ResponseResult.success(resultList).setPage(page);
    }

}
